from django.shortcuts import render
from django.views import View
from django.core.paginator import Paginator, PageNotAnInteger, EmptyPage, InvalidPage


from details.models import BlogContent


class IndexView(View):
    '''
    首页
    '''
    # 友情链接类型
    def get(self, request):
        article = BlogContent.objects.filter(user=1).order_by('-last_time')
        articles_list = [i for i in article]
        paginator = Paginator(articles_list, 10)


        page = 1
        try:
            page = paginator.page(page)
        #     如果请求不是整数页，返回第一页
        except PageNotAnInteger:
            page = paginator.page(1)
        # 如果访问页存在,返回第一页
        except InvalidPage:
            page = paginator.page(1)
        except EmptyPage:
            page = paginator.page(paginator.num_pages)

        return render(request,'blog/htmls/calmlog-index.html',{'page':page})


class NextPage(View):
    """
    翻页
    """
    def get(self, request,page):
        article = BlogContent.objects.filter(user=1).order_by('-last_time')
        articles_list = [i for i in article]
        paginator = Paginator(articles_list, 10)

        if page == "":
            page = 1
        try:
            page = paginator.page(page)
        #     如果请求不是整数页，返回第一页
        except PageNotAnInteger:
            page = paginator.page(1)
        # 如果访问页存在,返回第一页
        except InvalidPage:
            page = paginator.page(1)
        except EmptyPage:
            page = paginator.page(paginator.num_pages)

        return render(request,'blog/htmls/calmlog-index.html',{'page':page})







